package com.netease.login.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.netease.login.service.UserService;
import com.netease.meta.LoginCommand;
import com.netease.meta.User;

@Controller
public class LoginController {
	@Autowired
	private UserService userService;
	@RequestMapping(value = "/index.html")
	private String loginPage(){
		return "login";		
	}
   @RequestMapping(value = "/loginCheck.html")
   public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
	   boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getUserPassword());
	   if(!isValidUser){
		  return new ModelAndView("error");
	   }else{
		   User user = userService.findUserByUserName(loginCommand.getUserName());
		   request.getSession().setAttribute("user", user);
		   return new ModelAndView("check");
	   }
   }
   @RequestMapping(value = "/user.html")
   public String userPage(HttpServletRequest request){
	   User user = (User) request.getSession().getAttribute("user");
	   if(user == null){
		  return "unlogin";
	   }else{
		   return "user";
	   }
	   
	   
   }
}
